package com.component.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import com.component.dto.CalendarioDTO;
import com.component.dto.CargaInicialCtrlEjecDTO;

public class ControlDeEjecucionDAO {
	private static Logger log = Logger.getLogger(ControlDeEjecucionDAO.class);	
	
	public List getControlDeEjecucion(String IdProcInstancia) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
		List listReporte = new ArrayList();
		conexionMySQL connDao = new conexionMySQL();
		Connection conn = connDao.getConnection();	

		String query = "SELECT " +
				" DATE_FORMAT(CAL.FECHA_EJECUCION, '%Y-%m-%d'), " +
				" CAL.HORA_EJECUCION," +
				" CAL.ID_CALENDARIO," +
				" CS.ID_ACT_CSW," +
				" CS.ACP_CATALOGO_CSW_ID_CATALOGO_CSW," +
				" CS.orden" +
				" FROM sacpro.acp_calendario CAL," +
				" sacpro.acp_actividad ACT," +
				" acp_act_csw CS" +
				" where CAL.ID_PROC_INSTANCIA = "+ IdProcInstancia +
				" AND CAL.ORDEN_EJECUCION <> 0" +
				" AND ACT.ID_TIPO_ACT_PARAMETRO = 1" +
				" AND CAL.ID_ACTIVIDAD = ACT.ID_ACTIVIDAD" +
				" AND ACT.ID_ACTIVIDAD = ACP_ACTIVIDAD_ID_ACTIVIDAD" +
				" ORDER BY CS.ORDEN";
		try {
			Statement stmt = null;
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(query);
			while (rs.next()) {
				CargaInicialCtrlEjecDTO cargaDTO = new CargaInicialCtrlEjecDTO();				
				cargaDTO.setFechaEjecucion(rs.getString(1));
				cargaDTO.setHoraEjecucion(rs.getString(2));
				cargaDTO.setIdCalendario(rs.getString(3));
				cargaDTO.setIdActCSW(rs.getString(4));     //idActCSW
				cargaDTO.setAcpCatalogoCSWID(rs.getString(5));  //acpCatalogoCSWID
				cargaDTO.setOrden(rs.getString(6)); //orden
				log.info("Se encuentra tarea para ejecucion ID Calendario:"+rs.getString(3));
				listReporte.add(cargaDTO);	            	
			}
			rs.close();
			stmt.close();
		} catch (SQLException ex) {
			ex.printStackTrace();
			log.error("Error CalendarioDAO.getControlDeEjecucion SQLException: " +ex.getMessage());
		} finally {
			connDao.desconecta(conn);
		}
		return listReporte;
	}
	
	
	public List setControlDeEjecucion(String IdCalendario, String IdCatalogoCSW, String orden, String horaInicio, String fechaInicio) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
		List listReporte = new ArrayList();
		conexionMySQL connDao = new conexionMySQL();
		Connection conn = connDao.getConnection();	
		String calendario = "calendario";

		int IDIngreso = connDao.getLastRow("ID_CONTROL_EJECUCION","acp_control_ejecucion");
		IDIngreso++;

		String query = "INSERT INTO sacpro.acp_control_ejecucion (" +
				" ID_CONTROL_EJECUCION," +
				" IND_ORIGEN," +
				" ID_ESTADO_PARAMETRO," +
				" ACP_CALENDARIO_ID_CALENDARIO," +
				" ACP_CATALOGO_CSW_ID_CATALOGO_CSW," +
				" HORA_INICIO," +
				" FECHA_INICIO," +				
				" ORDEN)" +
				" VALUES ("+IDIngreso+",'"+calendario+"',1,"+IdCalendario+","+IdCatalogoCSW+","+horaInicio+",'"+fechaInicio+"',"+orden+")";
		try {
			Statement stmt = null;
			stmt = conn.createStatement();
			stmt.execute(query);			
			stmt.close();
		} catch (SQLException ex) {
			ex.printStackTrace();
			log.error("Error CalendarioDAO.setControlDeEjecucion SQLException: " +ex.getMessage());
		} finally {
			connDao.desconecta(conn);
		}
		return listReporte;
	}
	
	public static void main(String[] args) {
	

				

	}
	
	
}
